define(function (require, exports, module) {
    var angular = require('angular');
    require("angular-ui");
    require("angular-route");
    var util = require('util');
    
    var beeActivityModule = angular.module('beeActivity', [])
        .constant('api.path.activity', "/api/index.php?r=xmf/xmf-activity/")
        .factory('ActivityService', [
            '$http',
            'api.path.activity',
            function ($http, api_path) {
                var httpClient = {
                    get: function (api, data) {
                        var url = api_path + api;
                        var search = (data && data.search) || {};
                        angular.forEach(search, function (val, key) {
                            url += "&" + key + "=" + val;
                        });

                        return $http.get(url)
                            .then(function (res) {
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    },
                    post: function (api, data) {
                        return $http.post(api_path + api, (data && data.data) || {})
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    }
                }
                return {
                    getActivityList: function (data) {
                        return httpClient.post('activity-list', data)
                    },
                    createActivity:function(data){
                        return httpClient.post('list', data)
                    },
                    updateActivity:function(data){
                        return httpClient.post('list', data)
                    },
                    deleteActivity: function(data){
                        return httpClient.post('del-activity', data)
                    }
                }
            }
        ])
        .controller('ActivityController', [
            '$scope',
            '$rootScope',
            '$window',
            'ActivityService',
            function ($scope, $rootScope,$window, service) {
                $scope.pager = {
                    pageIndex: 1,
                    pageSize: 10,
                    changed: function (pageIndex) {
                        $scope.pager.pageIndex = pageIndex;
                        $scope.getActivitys({
                            pageIndex: $scope.pager.pageIndex,
                            pageSize: $scope.pager.pageSize
                        });
                    }
                }

                $scope.getActivitys = function (opts, keyword) {

                    var pageIndex = opts.pageIndex || $scope.pager.pageIndex || 1;

                    service.getActivityList({
                        data: {
                            "page_index": pageIndex,
                            "page_size": opts.pageSize || 10,
                            "proj_id": $scope.proj_id,
                            "key_word": keyword || ''
                        }
                    }).then(function (res) {
                        $scope.activityList = res.data.data;
                        $scope.pager.total = res.data.total;
                    });
                };

                $scope.deleteActivity = function (activity) {
                    service.deleteActivity({
                        data: {
                            'act_id': activity.t_activityId,
                        }
                    }).then(function (res) {
                        $rootScope.$emit('success');
                        $scope.pager.pageIndex = 1;
                        $scope.getActivitys($scope.pager);
                    });
                };
                
                $scope.$on('projChange', function (e, projId) {
                    $scope.proj_id = projId || $scope.$parent.curProjectId;
                    $scope.getActivitys($scope.pager);
                });

                function refresh () {
                    $scope.proj_id = $scope.$parent.curProjectId;
                    if ($scope.proj_id) {
                        $scope.getActivitys($scope.pager);
                    }
                }
                refresh();
                
                $scope.createActivity = function(){
                    $window.location.href = "/page/intermediary_organ/bee/activity/edit.html" + $window.location.search + "&pid=" + $scope.proj_id;
                };
                
                $scope.editActivity = function(activityId) {
                    $window.location.href = "/page/intermediary_organ/bee/activity/edit.html" + $window.location.search + "&id=" + activityId + "&pid=" + $scope.proj_id;
                };
                
                $scope.viewActivity = function(activityId) {
                    $window.location.href = "/page/intermediary_organ/bee/activity/view.html" + $window.location.search + "&id=" + activityId + "&pid=" + $scope.proj_id;
                };
                
                $scope.viewExpandBill = function() {
                     $window.location.href = "/page/ditch/index.shtml?code=guestAllot&token=" + util.getUrlPara('token') + '&proj_id=' + $scope.proj_id;
                };
            }
        ])
    
});
